package com.fhnew.app.ui.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResActivity;
import com.fhnew.app.model.network.entity.req.Resfuwenben;
import com.fhnew.app.ui.dialogactivity.SetingActivity;
import com.fhnew.app.util.ActivityUtil;
import com.fhnew.app.util.OnItemClickListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HelpActivity extends AppCompatActivity {

    private ImageView reback;
    private ImageView shezhi;
    private TextView zaixiankefu;
    private TextView qqkefu;
    private TextView weixinkefu;



    private TextView changjianwenti;
    private WebView webView;
    private RecyclerView helplist;
    private Adapter adapter;
    private ArrayList<ResActivity.DataBean.AppkefuBean.QqBean> qqBeans = new ArrayList<>();
    private ArrayList<ResActivity.DataBean.AppkefuBean.WxBean> wxBeans = new ArrayList<>();
    private int flag = 1;
    private ClipboardManager cm;
    private ClipData mClipData;
    private String id;
    private String url1;

    private void initView () {
        reback = findViewById(R.id.reback);
        shezhi = findViewById(R.id.shezhi);
        zaixiankefu = findViewById(R.id.zaixiankefu);
        qqkefu = findViewById(R.id.qqkefu);
        weixinkefu = findViewById(R.id.weixinkefu);
        changjianwenti = findViewById(R.id.changjianwenti);
        webView = findViewById(R.id.kefulist);
        helplist = findViewById(R.id.helplist);


        reback.setOnClickListener(onClickListener);
        shezhi.setOnClickListener(onClickListener);
        zaixiankefu.setOnClickListener(onClickListener);
        qqkefu.setOnClickListener(onClickListener);
        weixinkefu.setOnClickListener(onClickListener);
        changjianwenti.setOnClickListener(onClickListener);

    }

    /**
     * 客服
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        ActivityUtil.fullScreen(this);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_help);

        initView();

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayout.HORIZONTAL);
        helplist.setLayoutManager(layoutManager);
        adapter = new Adapter();
        helplist.setAdapter(adapter);
        getkefudata();
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许运行JavaScript
        webSettings.setDomStorageEnabled(true);

        //获取剪贴板管理器：
        cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

    }

    @Override
    protected void onPause () {
        super.onPause();
//        EventBus.getDefault().post(new ReloginEvent("100"));
    }

    private void getkefudata () {
        HttpJsonUtils.getHttpApiInstance()
                .getactivitydata(Constants.getuseAdress() + "api/m/lottery/getQPData")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResActivity>() {
                    @Override
                    protected void onCustomNext (ResActivity resActivity) {
                        if (resActivity.code == 0) {
                            id = resActivity.data.appqa;
                        }
                        if (resActivity.code == 0 && resActivity.data.appkefu != null) {
                            if (resActivity.data.appkefu.qq.size() != 0) {
                                qqBeans.clear();
                                qqBeans.addAll(resActivity.data.appkefu.qq);
                            }
                            if (resActivity.data.appkefu.wx.size() != 0) {
                                wxBeans.clear();
                                wxBeans.addAll(resActivity.data.appkefu.wx);
                            }
                        }
                        if (resActivity.data.onlineSer != null) {
                            if (resActivity.data.onlineSer.contains("https")) {
                                url1 = resActivity.data.onlineSer;
                                webView.loadUrl("" + resActivity.data.onlineSer);
                            } else {
//                                webView.loadUrl("https://www.baidu.com");
//                                url1 = "https://www.baidu.com";
                            }
                        }
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.reback:
                    finish();
                    break;
                case R.id.shezhi:
                    finish();
                    startActivity(new Intent(HelpActivity.this, SetingActivity.class));
                    break;
                case R.id.zaixiankefu:
                    btview();
                    webView.setVisibility(View.VISIBLE);
                    zaixiankefu.setBackgroundResource(R.mipmap.gamedown);
                    webView.loadUrl("" + url1);
                    break;
                case R.id.qqkefu:
                    btview();
                    flag = 1;
                    helplist.setVisibility(View.VISIBLE);
                    qqkefu.setBackgroundResource(R.mipmap.gamedown);
                    adapter.notifyDataSetChanged();
                    break;
                case R.id.weixinkefu:
                    btview();
                    flag = 2;
                    helplist.setVisibility(View.VISIBLE);
                    adapter.notifyDataSetChanged();
                    weixinkefu.setBackgroundResource(R.mipmap.gamedown);
                    break;
                case R.id.changjianwenti:
                    btview();
                    getSourceActivty();
                    changjianwenti.setBackgroundResource(R.mipmap.gamedown);
                    break;
            }
        }
    };


    private void getSourceActivty () {
        HttpJsonUtils.getHttpApiInstance()
                .getSourceActivty(Constants.getuseAdress() + "api/m/lottery/getSourceActivty", id + "", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resfuwenben>() {
                    @Override
                    protected void onCustomNext (Resfuwenben resActivity) {
                        webView.loadDataWithBaseURL(null, resActivity.data.APPqa, "text/html", "utf-8", null);
                        webView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    protected void onSubscribeCustom (Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom (Throwable e) {

                    }
                });
    }

    private void btview () {
        helplist.setVisibility(View.GONE);
        webView.setVisibility(View.GONE);
        zaixiankefu.setBackgroundResource(R.mipmap.gameup);
        qqkefu.setBackgroundResource(R.mipmap.gameup);
        weixinkefu.setBackgroundResource(R.mipmap.gameup);
        changjianwenti.setBackgroundResource(R.mipmap.gameup);
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(HelpActivity.this)
                    .inflate(R.layout.helpitem, viewGroup, false));
        }

        @Override
        public void onBindViewHolder (final Adapter.MViewHolder holder, final int position) {

            if (flag == 1) {
                holder.helpnumber.setText("" + qqBeans.get(position).account);
                Glide.with(HelpActivity.this).load(qqBeans.get(position).erweima).placeholder(R.mipmap.nodataimg).into(holder.helpimg);
                holder.helpbt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        //大图
//                        ImagePreview
//                                .getInstance()
//                                .setContext(HelpActivity.this)
//                                .setIndex(0)
//                                .setImage(qqBeans.get(position).erweima)
//                                .start();
                        Toast.makeText(HelpActivity.this, "已复制到剪切板", Toast.LENGTH_LONG).show();
                        mClipData = ClipData.newPlainText("Label", qqBeans.get(position).account);
                        // 将ClipData内容放到系统剪贴板里。
                        cm.setPrimaryClip(mClipData);
                    }
                });
            } else {
                holder.helpnumber.setText("" + wxBeans.get(position).account);
                Glide.with(HelpActivity.this).load(wxBeans.get(position).erweima).placeholder(R.mipmap.nodataimg).into(holder.helpimg);
                holder.helpbt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick (View v) {
                        // 创建普通字符型ClipData
                        Toast.makeText(HelpActivity.this, "已复制到剪切板", Toast.LENGTH_LONG).show();
                        mClipData = ClipData.newPlainText("Label", wxBeans.get(position).account);
                        // 将ClipData内容放到系统剪贴板里。
                        cm.setPrimaryClip(mClipData);
                    }
                });
            }


        }

        @Override
        public int getItemCount () {
            if (flag == 1) {
                return qqBeans.size();
            } else {
                return wxBeans.size();
            }
        }

        @Override
        public void onItemClick (View view, int position) {

        }

        public class MViewHolder extends RecyclerView.ViewHolder {
            ImageView helpimg;
            TextView helpnumber;
            TextView helpbt;

            MViewHolder (View itemView) {
                super(itemView);
                helpimg = (ImageView) itemView.findViewById(R.id.helpimg);
                helpnumber = (TextView) itemView.findViewById(R.id.helpnumber);
                helpbt = (TextView) itemView.findViewById(R.id.helpbt);
            }
        }
    }
}
